package com.example.a_java.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.dto.RepaymentRecordDTO;
import com.example.a_java.service.RepaymentRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 还款记录管理控制器
 */
@Tag(name = "还款记录管理", description = "还款记录管理相关接口")
@RestController
@RequestMapping("/repayment/record")
@SecurityRequirement(name = "bearer-key")
public class RepaymentRecordController {
    
    @Autowired
    private RepaymentRecordService repaymentRecordService;
    
    /**
     * 获取还款记录列表
     */
    @Operation(summary = "获取还款记录列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('repayment:record:list')")
    public Result<Page<Map<String, Object>>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "贷款编号") @RequestParam(required = false) String loanNo,
            @Parameter(description = "合同编号") @RequestParam(required = false) String contractNo,
            @Parameter(description = "客户姓名") @RequestParam(required = false) String customerName,
            @Parameter(description = "还款方式") @RequestParam(required = false) String payMethod,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate) {
        
        IPage<Map<String, Object>> page = repaymentRecordService.getRepaymentRecordList(
                new Page<>(pageNum, pageSize),
                loanNo,
                contractNo,
                customerName,
                payMethod,
                startDate,
                endDate);
        
        return Result.success((Page<Map<String, Object>>) page);
    }
    
    /**
     * 记录还款
     */
    @Operation(summary = "记录还款")
    @PostMapping
    @PreAuthorize("hasAuthority('repayment:record:add')")
    public Result<Map<String, Object>> recordRepayment(@RequestBody @Valid RepaymentRecordDTO recordDTO) {
        Map<String, Object> result = repaymentRecordService.recordRepayment(recordDTO);
        return Result.success(result);
    }
    
    /**
     * 获取还款详情
     */
    @Operation(summary = "获取还款详情")
    @GetMapping("/{recordId}")
    @PreAuthorize("hasAuthority('repayment:record:query')")
    public Result<Map<String, Object>> getRecordDetail(@PathVariable Long recordId) {
        Map<String, Object> detail = repaymentRecordService.getRepaymentRecordDetail(recordId);
        return Result.success(detail);
    }
}
